< previous page page_216 next page >

Page 216
The program rotates points in the input image around the image's center. It translates the result so that it is centered in the output image.
Figure 7.18 shows how to rotate the point (x, y) around the origin through the angle f. The distance R from (cx, cy) to (x, y) is:
e10a6a0a59f035e870d90cca05b85fce.gif
R = Sqr(dx * dx + dy * dy)
Using the arctangent function, the program can calculate the angle q = arctan(y/x). Unfortunately, Visual Basic's arctangent function Atn returns only values between p/2 and p/2. The ATan2 function shown in the following code uses Atn to return values for angles that fall outside this range.
e10a6a0a59f035e870d90cca05b85fce.gif
' Return the arctan of dy/dx.
Public Function ATan2( _
    ByVal dy As Single, ByVal dx As Single) As Single
Const PI = 3.14159265

Dim theta As Single

    If Abs(dx) < 0.01 Then
        If dy < 0 Then
            theta = -PI / 2
        Else
            theta = PI / 2
        End If
    Else
        theta = Atn(dy / dx) 
        If dx < 0 Then theta = PI + theta
    End If
     
    ATan2 = theta
End Function
z0236-01.gif
Figure 7.18
Rotating a point through the angle 
f.

 
< previous page page_216 next page >